package com.seas.service.impl;

import com.seas.dao.PersonRepository;
import com.seas.domain.Person;
import com.seas.service.TransactionDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Seas Cheng on 2017/9/26
 */
@Service
public class TransactionDemoServiceImpl implements TransactionDemoService {
    @Autowired
    PersonRepository personRepository;

    @Transactional(noRollbackFor = {IllegalArgumentException.class})
    public Person savePersonWithRollBack(Person person) {
        Person p = personRepository.save(person);
        if (person.getName().equals("cheng")){
            throw new IllegalArgumentException("cheng 已经存在，回滚数据");
        }
        return p;
    }

    @Transactional(noRollbackFor = {IllegalArgumentException.class})
    public Person savePersonWithoutRollback(Person person) {
        Person p = personRepository.save(person);
        if (person.getName().equals("cheng")){
            throw new IllegalArgumentException("cheng 已经存在，不数据");
        }
        return p;
    }
}
